package org.jmc.refmanager.loader.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public final class TimeUtil {
	public final static long ONE_SECOND = 1000L;
	public final static long SECONDS = 60L;

	public final static long ONE_MINUTE = ONE_SECOND * SECONDS;
	public final static long MINUTES = 60L;

	public final static long ONE_HOUR = ONE_MINUTE * MINUTES;
	public final static long HOURS = 24L;

	public final static long ONE_DAY = ONE_HOUR * HOURS;

	private TimeUtil() {
	}

	/**
	 * converts time (in milliseconds) to human-readable format
	 * "<w> days, <x> hours, <y> minutes and (z) seconds"
	 * 
	 * @param timestamp Start time
	 * @param endTimestamp End time
	 */
	public static String getDuration(long timestamp, long endTimestamp) {
	    if(timestamp != 0 && endTimestamp != 0)
	    {
	        try
	        {
	            long duration = endTimestamp - timestamp;
	            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss:SSS");
	            format.setTimeZone(TimeZone.getTimeZone("GMT"));
	            return format.format(new Date(duration));
	        }
	        catch (RuntimeException e)
	        {
	        	Util.traceException(e);	
	        }
	    }
	 
	    return "";
	}
}
